package com.neusoft.cmsprojects.action;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.neusoft.cmsprojects.model.Privilege;
import com.neusoft.cmsprojects.model.Role;
import com.neusoft.cmsprojects.service.PrivilegeService;
import com.neusoft.cmsprojects.util.ResponseUtil;
import com.opensymphony.xwork2.ActionSupport;

public class PrivilegeAction implements RequestAware,SessionAware {

	private static final long serialVersionUID = 1L;

	private Privilege privilege;
	
	@Resource(name="privilegeservice")
	private PrivilegeService service;
	
	private Map session;
	private Map request;
	//一级菜单的id
	private Integer parentId;
	private HttpServletResponse response;
	//判断是否移动为一级菜单
	private int ToOne;
	/**
	 * 权限列表 ,准备数据
	 * 方法名：list<BR>
	 * 创建人：许耀东 <BR>
	 * 时间：2014年12月24日-上午9:56:12 <BR>
	 * @return String<BR>
	 */
	public String list(){
//		List<Object> list=service.findAll();
		List<Privilege> list=service.findTopPrivilege();
		request.put("privilege", list);
		return "list";
	}
	
	/**
	 * 跳转添加权限页面,准备一级菜单数据
	 * 方法名：addPage<BR>
	 * 创建人：许耀东 <BR>
	 * 时间：2014年12月24日-上午9:57:07 <BR>
	 * @return String<BR>
	 */
	public String addPage(){
		List<Privilege> list=service.findTopPrivilege();
		session.put("topprivilegeadd", list);
		return "addPage";
	}

	/*
	 * 保存一级菜单
	 */
	public String saveOne() throws Exception{
		response= ServletActionContext.getResponse();
		service.save(privilege);
		ResponseUtil.write(response, "success");
		return null;
	}
	
	/*
	 * 保存二级菜单
	 */
	public String savetwo() throws Exception{
		response= ServletActionContext.getResponse();
		Privilege p=service.getById(parentId);
		privilege.setParent(p);
		service.save(privilege);
		ResponseUtil.write(response, "success");
		return null;
	}
	
	/*
	 * 跳转到修改二级菜单页面
	 */
	public String updatePage(){
		privilege=service.getById(privilege.getId());
//		System.out.println(privilege.getParent().getId());
		List<Privilege> list=service.findTopPrivilege();
		request.put("privilege", privilege);
		request.put("topprivilege", list);
		return "updatePage";
	}
	/*
	 * 更新二级菜单的信息
	 */
	public String updateTwoMenu() throws Exception{
		response= ServletActionContext.getResponse();
		if(ToOne==1){
			Privilege p=service.getById(parentId);
			privilege.setParent(p);
		}
		service.merge(privilege);
		ResponseUtil.write(response, "success");
		return null;
	}
	
	/*
	 * 跳转到一级菜单修改页面
	 */
	public String updatePageOneMenu(){
		privilege=service.getById(privilege.getId());
		List<Privilege> list=service.findTopPrivilege();
		request.put("topprivilege", list);
		request.put("privilege", privilege);
		return "updatePageOneMenu";
	}
	/*
	 * 更新一级菜单信息
	 */
	public String updateOneMenu() throws Exception{
		response= ServletActionContext.getResponse();
		if(parentId!=0){
			Privilege p=service.getById(parentId);
			privilege.setParent(p);
		}
		service.merge(privilege);
		ResponseUtil.write(response, "success");
		return null;
	}
	
	/*
	 * 删除菜单
	 */
	public String delete() throws Exception{
		response= ServletActionContext.getResponse();
		service.delete(privilege);
		ResponseUtil.write(response, "success");
		return null;
	}
	
	//setter getter
	public Privilege getPrivilege() {
		return privilege;
	}

	public void setPrivilege(Privilege privilege) {
		this.privilege = privilege;
	}

	@Override
	public void setSession(Map session) {
		this.session=session;
	}

	@Override
	public void setRequest(Map request) {
		this.request =request;
	}

	public Integer getParentId() {
		return parentId;
	}

	public void setParentId(Integer parentId) {
		this.parentId = parentId;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}
	
}
